// pages/miaosha/miaosha.js
var api = require('../../api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgServer :  api.default.img_server,
        allData:[],//接口返回的所有数据
        time_list: [],
        currentTime:{//当前active的时间点
            // id:null,
            // name: "07:00",
            // status: 2,
            // status_text: "即将开始",
            // isActive:true
        },
        activity_list:[
            {id:"",title:"",product_list:[]},
            {id:"",title:"",product_list:[]},
        ],
        over_time: 0,//距离结束时间的毫秒数
        activeActivityItems: [],
        moneyIcon:api.MONEYICON,
        moneyIconOther:api.MONEYICONOTHER,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let page = this;
        if(options.mid){
            app.request({
                url: api.default.getFlashList+options.mid,
                success:function(res){
                  if(res.code ==200 && res.data.length>0){ 
                    page.getCurrentTime(res.data);
                  }
                }
              })
        }
    },
    getCurrentTime(res){
        let page = this;
        let data = res[0];
        let temptime_list= [];
        let currentActiveTime =null;
        let session = data.flashPromotionProductSessions;
        let tempover_time =0;
        let testIndex = 0;
    //  let now1 = new Date();
    //  let nowTime1 = now1.getTime();
    //   console.log("nowTime1")
    //   console.log(nowTime1);
    //   console.log(new Date("2020-02-09"))
    //   console.log(new Date("2020-02-09").getTime());
    //   console.log("~~~~~~~~~~~~~~~~~~~~~22")
        for(const item in session){
            let now =null;
            res.map((item1)=>{
                now=new Date(item1.systemTime);
            }) 
            testIndex++;
            //now = new Date(item.systemTime);
            let nowTime = now.getTime();
            let year = now.getFullYear();
            let month = now.getMonth();
            let date = now.getDate();
            //let startTimeStr = year+"-"+month+"-"+date+" "+ session[item].startTime;
          let starthour = session[item].flashPromotionSession.startTime.split(":")[0];
          let startminute = session[item].flashPromotionSession.startTime.split(":")[1];
          let startsecond = session[item].flashPromotionSession.startTime.split(":")[2];
          let startTime = new Date(year, month, date, starthour, startminute, startsecond).getTime();
          console.log("startTime:" + startTime)
           // let endTimeStr = year+"-"+month+"-"+date+" "+ session[item].endTime;
          let endhour = session[item].flashPromotionSession.endTime.split(":")[0];
          let endminute = session[item].flashPromotionSession.endTime.split(":")[1];
          let endsecond = session[item].flashPromotionSession.endTime.split(":")[2];
          let endTime = new Date(year, month, date, endhour, endminute, endsecond).getTime();
          console.log("endTime:" + endTime);
          console.log((nowTime > startTime) && (nowTime < endTime));
            //let endTime = new Date(endTimeStr).getTime();
            let tempstatus_text="";
            let tempisActive= false;
            let tempstatus = 0;//1，即将开始 2，进行中 3，已结束
            if(nowTime < startTime){
                tempstatus_text="即将开始";
                tempstatus=1;
            }else if(nowTime > startTime && nowTime<endTime){
                tempstatus_text="进行中";
                tempisActive=true;
                tempstatus = 2;
                tempover_time = endTime - nowTime;
                
                currentActiveTime={
                    id: session[item].id,
                    name: session[item].flashPromotionSession.name,
                    status: 2,
                    startTime:session[item].flashPromotionSession.startTime,
                    endTime:session[item].flashPromotionSession.endTime,
                    status_text: tempstatus_text,
                    isActive:tempisActive
                }
               
            }else{
                tempstatus_text="已结束";
                tempstatus= 3;
            }
            temptime_list.push({
              id: session[item].id,
              name: session[item].flashPromotionSession.name,
              status: tempstatus,
              startTime:session[item].flashPromotionSession.startTime,
              endTime:session[item].flashPromotionSession.endTime,
              status_text: tempstatus_text,
              isActive:tempisActive,
            })
        }
        console.log(currentActiveTime);
        if(currentActiveTime && currentActiveTime.id){
            page.setData({
                allData:data,
                time_list:temptime_list,
                currentTime:currentActiveTime,
                over_time:tempover_time
            });
        }else{
            page.setData({
                allData:data,
                time_list:temptime_list,
                //currentTime:currentActiveTime,
                over_time:tempover_time
            });
        }
        
        // console.log("@#$$$$$$$$%%%%%%%%%%***********")
        // console.log(tempover_time);
        // console.log("11111");
        // console.log(testIndex);
        // console.log("@@@@2222")
        console.log(page.data.currentTime);
        
        // console.log(currentActiveTime.id);
      page.getProductList(currentActiveTime.id);
       //page.getProductList(5);
    },
    getProductList(cid){//因为setData以后，页面传输需要时间，所以直接把id传过来
        let page = this;
        //let curActiveTime= page.data.currentTime;
        let curId =  cid;
        let allData = page.data.allData.flashPromotionProductSessions;
        let activity_list=[];
        let endTime= "";
        let tempactiveActivityItems=[];
        for(const item of allData){
            let tempobj={
                id:item.id,
                title:page.data.allData.title,
            }
            tempactiveActivityItems.push(item.id)
            // let session = item.flashPromotionProductSessions;
            // for(const idx in session){
            //     if(session[idx].id == curId){
            //         tempobj.product_list = session[idx].flashPromotionSession.flashProducts;
            //         endTime = session[idx].flashPromotionSession.endTime;
            //     }
            // }
            if(item.id == curId){
                tempobj.product_list = item.flashPromotionSession.flashProducts
                endTime = item.flashPromotionSession.endTime;
            }
            activity_list.push(tempobj)
        }
        page.setData({
          endTime:endTime,
          activity_list:activity_list,
          curActiveIndex: curId
            //activeActivityItems:tempactiveActivityItems
        });
        if(page.data.activity_list.length>0){
            page.setData({
                activeActivityItems:tempactiveActivityItems
            });
            console.log(page.data.activeActivityItems);
        }
    },
    timeChange(event){
        let page = this;
        let newitem = event.currentTarget.dataset.item;
        newitem.isActive = true;
        //let newindex =  page.data.time_list.indexOf(newitem);
        // let oldItem = page.data.currentTime
        // let oldIndex = page.data.time_list.indexOf(oldItem);
        let temptime_list = page.data.time_list;
       for(const item of temptime_list){
            if(item.id == newitem.id){
                item.isActive = true;

            }else{
                item.isActive = false
            }
       }
        page.setData({
            time_list:temptime_list,
            currentTime:newitem,
        });
        page.getProductList(newitem.id);
    },
    onChange(event) {
        this.setData({
            activeActivityItems: event.detail
        });
      },
      onCountDown(e){
        this.setData({
          timeData: e.detail
        });
        let { hours, minutes, seconds } = e.detail
        if (hours == 0 && minutes == 0 && seconds == 0) {
          this.setData({
            'currentTime.status': 0,//当前时间段的秒杀结束 
          })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})